//
//  KLSharePresentController.m
//  KunLun
//
//  Created by Mac on 2017/11/29.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLSharePresentController.h"

@interface KLSharePresentController ()
@property(nonatomic, strong) STButton                     *clearButton;
@property(nonatomic, strong) UIView                     *whiteView;
@property(nonatomic, copy) void(^shareHandle)(NSString * chosedStr)                   ;
@end

@implementation KLSharePresentController

- (instancetype)initWithHandle:(void (^)(NSString *))handle
{
    if (self == [super init]) {
        self.modalPresentationStyle = UIModalPresentationOverFullScreen;
        self.shareHandle  = handle;
    }
    return self;
}
#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    [self configSubView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self show];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    self.clearButton = [[STButton alloc] initWithFrame:UIScreenFrame
                                                 title:nil
                                            titleColor:nil
                                             titleFont:0
                                          cornerRadius:0
                                       backgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.4]
                                       backgroundImage:nil
                                                 image:nil];
    [weakSelf.clearButton setClicAction:^(UIButton *sender) {
        [weakSelf dismiss];
    }];
    [self.view addSubview:_clearButton];
    
    UIView * whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, UIScreenHeight, UIScreenWidth, 240)];
    whiteView.backgroundColor = [UIColor whiteColor];
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(10, 20, UIScreenWidth - 20, 20)
                                                     text:@"请选择分享平台"
                                                textColor:KL_firstTextColor
                                                     font:15
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    [whiteView addSubview:titleLable];
    
    
    
    STButton * qqButton = [[STButton alloc] initWithFrame:CGRectMake(15, titleLable.bottom + 30, 32, 34)
                                                    title:@"QQ"
                                               titleColor:KL_firstTextColor
                                                titleFont:0
                                             cornerRadius:0
                                          backgroundColor:[UIColor whiteColor]
                                          backgroundImage:[UIImage imageNamed:@"分享-qq"]
                                                    image:nil];
    [qqButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedConfimButton:sender];
    }];
    qqButton.titleEdgeInsets = UIEdgeInsetsMake(qqButton.height, 0, 0, 0);
    qqButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
   // [whiteView addSubview:qqButton];
    
    STLabel * qqLable = [[STLabel alloc] initWithFrame:CGRectMake(0, qqButton.bottom + 15, UIScreenWidth / 3, 20)
                                                  text:@"QQ"
                                             textColor:KL_firstTextColor
                                                  font:15
                                           isSizetoFit:NO
                                         textAlignment:NSTextAlignmentCenter];
    qqLable.left = 0;
 //   [whiteView addSubview:qqLable];
    
    
    STButton * wechatButton = [[STButton alloc] initWithFrame:CGRectMake(15, titleLable.bottom + 30, 42, 34)
                                                        title:@"微信好友"
                                                   titleColor:KL_firstTextColor
                                                    titleFont:0
                                                 cornerRadius:0
                                              backgroundColor:nil
                                              backgroundImage:[UIImage imageNamed:@"分享-微信"]
                                                        image:nil];
    [wechatButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedConfimButton:sender];
    }];
    wechatButton.titleEdgeInsets = UIEdgeInsetsMake(qqButton.height, 0, 0, 0);
    wechatButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [whiteView addSubview:wechatButton];
    STLabel * wechatLable = [[STLabel alloc] initWithFrame:CGRectMake(0, qqButton.bottom + 15, UIScreenWidth / 3, 20)
                                                      text:@"微信好友"
                                                 textColor:KL_firstTextColor
                                                      font:15
                                               isSizetoFit:NO
                                             textAlignment:NSTextAlignmentCenter];
    wechatLable.left = 0;
    [whiteView addSubview:wechatLable];
    
    
    STButton * frinedButton = [[STButton alloc] initWithFrame:CGRectMake(15, titleLable.bottom + 30, 34, 34)
                                                        title:@"朋友圈"
                                                   titleColor:KL_firstTextColor
                                                    titleFont:0
                                                 cornerRadius:8
                                              backgroundColor:nil
                                              backgroundImage:[UIImage imageNamed:@"分享朋友圈"]
                                                        image:nil];
    [frinedButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedConfimButton:sender];
    }];
    frinedButton.titleEdgeInsets = UIEdgeInsetsMake(qqButton.height, 0, 0, 0);
    frinedButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [whiteView addSubview:frinedButton];
    STLabel * frinedLable = [[STLabel alloc] initWithFrame:CGRectMake(0, qqButton.bottom + 15, UIScreenWidth / 3, 20)
                                                      text:@"朋友圈"
                                                 textColor:KL_firstTextColor
                                                      font:15
                                               isSizetoFit:NO
                                             textAlignment:NSTextAlignmentCenter];
    frinedLable.left = 2 * UIScreenWidth / 3;
    [whiteView addSubview:frinedLable];
    
    
    qqButton.centerX = qqLable.centerX;
    wechatButton.centerX = wechatLable.centerX;
    frinedButton.centerX = frinedLable.centerX;
    
    STButton * comfimButton = [[STButton alloc] initWithFrame:CGRectMake(40, 10, UIScreenWidth - 80, 40)
                                                        title:@"取消分享"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:15
                                                 cornerRadius:8
                                              backgroundColor:KL_BlueBackGroundColor
                                              backgroundImage:nil
                                                        image:nil];
    [comfimButton setClicAction:^(UIButton *sender) {
        [weakSelf dismiss];
    }];
    comfimButton.bottom = whiteView.height - 20;
    comfimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [whiteView addSubview:comfimButton];
    
    
    [self.clearButton addSubview:whiteView];
    _whiteView = whiteView;
}

#pragma mark --Action Method
- (void)onSelectedConfimButton:(UIButton*)sneder{
    [UIView animateWithDuration:0.25 animations:^{
        self.whiteView.top = UIScreenHeight;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
            if (self.shareHandle) {
                self.shareHandle(sneder.currentTitle);
            }
        }];
    }];
    
}
- (void)dismiss{
    [UIView animateWithDuration:0.25 animations:^{
        self.whiteView.top = UIScreenHeight;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}

- (void)show{
    [UIView animateWithDuration:0.25 animations:^{
        self.whiteView.bottom = UIScreenHeight;
    }];
}
@end

